package org.finesys.codegen.controller;

import java.util.List;

import org.finesys.codegen.dto.GenTemplateGroupDTO;
import org.finesys.codegen.entity.GenGroup;
import org.finesys.codegen.service.GenGroupService;
import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.module.R;
import org.finesys.common.core.util.ExcelUtil;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/group")
@Tag(name = "组管理")
public class GenGroupController {

    private final GenGroupService genGroupService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param genGroup 组
     * @return
     */
    @GetMapping(RestApiConfig.PAGE)
    @Operation(summary = "分页查询", description = "分页查询")
    public R<?> page(Page<GenGroup> page, GenGroup genGroup) {
        return R.success(genGroupService.findPage(page, genGroup));
    }

    @Operation(summary = "查询列表", description = "查询列表")
    @GetMapping(RestApiConfig.LIST)
    public R<?> list(GenGroup genGroup) {
        return R.success(genGroupService.list(Wrappers.query(genGroup)));
    }


    @PostMapping
    @Operation(summary = "新增", description = "新增")
    @LogAnnotation(title = "新增组", operateType = OperateType.INSERT)
    public R<?> add(@RequestBody GenTemplateGroupDTO genTemplateGroupDTO) {
        return R.judge(genGroupService.saveGroup(genTemplateGroupDTO));
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    @LogAnnotation(title = "修改组", operateType = OperateType.UPDATE)
    public R<?> update(@RequestBody GenTemplateGroupDTO genTemplateGroupDTO) {
        return R.judge(genGroupService.updateGroup(genTemplateGroupDTO));
    }

    @DeleteMapping
    @Operation(summary = "删除", description = "删除")
    @LogAnnotation(title = "删除组", operateType = OperateType.DELETE)
    public R<?> delete(@RequestBody Long[] ids) {
        return R.success(genGroupService.deleteGroup(ids));
    }

    @GetMapping("/{id}")
    @Operation(summary = "通过id查询", description = "通过id查询")
    public R<?> get(@PathVariable Long id) {
        return R.success(genGroupService.getGenTemplateGroupDTO(id));
    }

    @GetMapping(RestApiConfig.DETAILS)
    @Operation(summary = "通过条件查询", description = "通过条件查询")
    public R<?> details(GenGroup genGroup) {
        return R.success(genGroupService.getDetail(genGroup));
    }

    @GetMapping(RestApiConfig.EXPORT_EXCEL)
    public void export(GenGroup genGroup) {
        List<GenGroup> list = genGroupService.findList(genGroup);
        ExcelUtil<GenGroup> excelUtil = new ExcelUtil<GenGroup>(GenGroup.class);
        excelUtil.exportExcel(list, "组管理", "组管理");
    }
}
